home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 669 < prev    next >
Encoding:
Text File  |  1996-08-05  |  7.8 KB  |  200 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: techfak.uni-bielefeld.de!isthesin
  3. From: isthesin@techfak.uni-bielefeld.de (Stephan Thesing)
  4. Subject: Re: PPC compilers
  5. Message-ID: <DKyqp9.CIL@hermes.hrz.uni-bielefeld.de>
  6. Sender: isthesin@TechFak.Uni-Bielefeld.DE (Stephan Thesing)
  7. Date: Wed, 10 Jan 1996 11:31:56 GMT
  8. References: <john.hendrikx.40ka@grafix.xs4all.nl> <jasonb.820051107@cs.uwa.edu.au> <4c9i2l$h3i@sunsystem5.informatik.tu-muenchen.de> <4ck47h$g07@maureen.teleport.com> <19960106.4EE928.CF59@sisyphus.demon.co.uk> <4cokkg$415@maureen.teleport.com> <19960107.533250.14585@sisyphus.demon.co.uk> <4cqrti$f6u@maureen.teleport.com> <jasonb.821173879@cs.uwa.edu.au>
  9. Nntp-Posting-Host: moos.techfak.uni-bielefeld.de
  10. Organization: Universitaet Bielefeld, Technische Fakultaet.
  11. X-Newsreader: xrn 8.01
  12.  
  13. In article <jasonb.821173879@cs.uwa.edu.au>, jasonb@cs.uwa.edu.au (Jason S Birch) writes:
  14. |> sschaem@teleport.com (Stephan Schaem) writes:
  15. |> 
  16. |> > polymorphism?
  17. |> 
  18. |> > int a,b,c;
  19. |> 
  20. |> > ...
  21. |> > a = b /c;
  22. |>         ^
  23. |> > a *= result;
  24. |>     ^
  25. |> The "/" and "*" are polymorphic - you can use them in any mathematical
  26. |> expression, without caring whether the left and right sides are chars,
  27. |> shorts, longs, floats, doubles, or any equivalent type. If they were
  28. |> not polymorphic, you might have, for example:
  29. |> a int= b int/ c;
  30. |> a int*= result;
  31. |> 
  32. |> This is essentially what you have in assembler. Note that with C++,
  33. |> you can overload "+", "-", "*", "/", "=", etc, to work with any custom
  34. |> classes as well, so you could have, for example:
  35. |> 
  36. |> vector a,b,c;
  37. |> real r;
  38. |> ...
  39. |> a = b + c;
  40. |> r = a * b;
  41. |> cout << "Values are: " << a << ", " << r << ".\n";
  42. |> 
  43. |> and have, say:
  44. |> 
  45. |> Values are: [3,7,3], 67.
  46. |> 
  47. |> as the output.
  48. |> 
  49. |> > struct.float = a;
  50. |> 
  51. |> This is polymorphism of "=", but it's also taking advantage of the
  52. |> fact that C can convert an integer into a float.
  53. |> 
  54.  
  55. [Somehow out of topic, but maybe interesting:]
  56.  
  57. In fact, this is mostly called 'ad hoc polymorphism'.
  58. Real polymorphism is (at least in functional languages) something like this:
  59.  
  60.    id x = x 
  61. A function id, the identity on everything.
  62. it's type is * -> *, saying it can take an argument of arbitrary type and
  63. gives something of the same type as result, e.g.
  64. id 'c' = 'c'
  65. id 5635635 = 5635635
  66. id [1,2,3,4,5] = [1,2,3,4,5], etc.pp
  67.  
  68. This is something different from ad-hoc-polymorphism, since there, for each 
  69. different argument type you have in fact a different operator, e.g.
  70. 1 + 2       + has type int x int -> int
  71. 1.3 + 4.5   + has type float x float -> float
  72. The same goes for overloading in C++
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. |> > I dunno , I just find it puzzling to declar variable without giving
  80. |> > a care of what its usage will be. You programing practice is VERY unwise...
  81. |> 
  82. |> Your misunderstanding is that you think he means he has no idea of
  83. |> what types each variable is, whereas what he's saying is he need have
  84. |> no idea of how each type is actually implemented on a particular
  85. |> machine/OS - all he has to do is know how those types are defined to
  86. |> behave under ANSI C. In fact, if you want to write portable code, you
  87. |> have to restrict your knowledge of behaviour of the types to that
  88. |> which is guaranteed under ANSI C. Taking advantage of any other
  89. |> information you might have (eg. endian-ness, number of bits) limits 
  90. |> your portability.
  91. |> 
  92.  
  93. Right, this is a difference between ASM and HLL:
  94. ASM is defined in operational terms, where everything is fixed (word size, bit order..)
  95. HLL are defined by the 'meaning' of a program, abstracting away from real
  96. hardware, just by looking at concepts as store, values, etc.
  97. How this meaning (semantics) is IMPLEMENTED on a real hardware is
  98. subject to the programmer, as long as he stays in the framework of the semantics.
  99. (e.g. he may choose to implement integers as BCD numbers. this is ok, as long as
  100. the behaviour of his implementation is the same as that defined by the semantics)
  101.  
  102.  
  103. |> >: I know better than to declare a value as unsigned long when I'm
  104. |> >: going to assign a clock_t to it.  And if I did, I'd lose the
  105. |> >: fractional part of a floating-point clock_t, not the upper 32 bits.
  106. |> 
  107. |> > You said yourself after peeking at the .h that clock_t was a ulong.
  108. |> > But you know better then not using a ulong... ? Should I assume
  109. |> > you alway use the largest data type available,double ? (What a waist)
  110. |> 
  111. |> No, you should use clock_t! If you want a variable to use in functions
  112. |> that expect a clock_t, you declare it thus:
  113. |> 
  114. |> clock_t Stephens_var;
  115. |> 
  116. |> You do *not* declare it as:
  117. |> 
  118. |> ulong Stephens_var;
  119. |> 
  120. |> even if you *know* that on your particular implementation, that's what
  121. |> it really is, because that restricts your portability.
  122. |> 
  123. |> > And you mean you would loose the fractional part of clock_t if you
  124. |> > used ulong , not the upper 32bits? This make absolutly no sense since
  125. |> > we are talking about interger types.(BTW copy 0x00000001 into a 2byte int,
  126. |> > you will loose the upper 16bit... )
  127. |> 
  128. |> You lose nothing if you declare it as clock_t.
  129. |> 
  130. |> > If the header file was programmed correctly it would not use ANSI data
  131. |> > type directly.... 
  132. |> 
  133. |> Why not? You should just know better than to look at it and take
  134. |> advantage of that information.
  135. |> 
  136. |> Incidentally, when I create a custom data type I want to keep private,
  137. |> I provide a public header file similar to:
  138. |> 
  139. |> typedef void *MyDataType;
  140. |> 
  141. |> MyDataType *MDT_Create(...);
  142. |> void MDT_Dispose(MyDataType *);
  143. |> 
  144. |> and inside the private header file, I actually define what MyDataType
  145. |> is. This way, no-one can "accidentally" use the internal definition,
  146. |> and I am free to change it at will, without having to worry about
  147. |> those who *don't* know better than to look at it and take advantage of
  148. |> the information.
  149.  
  150. This is why C++ was invented ;-)
  151.  
  152. |> 
  153. |> > The way you get optimal speed is optimizing the optimal algorithim.
  154. |> > If you stop at the algorithm you loose... Optimizing at this stage
  155. |> > will give an exponential result on the work you done on the algorithm.
  156. |> > And usually this stage is done in assembler.
  157. |> 
  158. |> The problem is, do you want portable code? If yes, then you should be
  159. |> aware that what might be optimal for one particular CPU using one
  160. |> particular compiler is not necessarily the same for another. For
  161. |> example, scanning through an array and adding the contents using array
  162. |> dereferencing and a moving pointer: On an 040 Amiga, the latter was
  163. |> faster; using gcc on a Sun, both produced the same assembler code; and
  164. |> using Metrowerks on a PowerMac, the former was actually quicker
  165. |> because the pointer method used post-incrementing (ie. sum += *ptr++)
  166. |> and the compiler wasn't smart enough to realize it didn't need to
  167. |> store the pre-incremented value and used an extra register to store
  168. |> it. The moral? Don't make assumptions about what is an optimization,
  169. |> and what isn't, unless you test it on every combination you plan to
  170. |> run it on.
  171. |> 
  172.  
  173. One may argue that portability is not necessary, if one knows on which
  174. machine the program will be run. 
  175. This is true, but unfortunately, as the Amiga line has shown, we can not
  176. be sure, if the next machine has the same CPU (it may even have a CPU from a different 
  177. family, as the PPC discussion shows..). 
  178. But surely you want your program to run on the new machine as well, at least
  179. if you plan to make it publically available.
  180. So you have to take into account the portability issue as well. HLL
  181. programmers can (but need not) write portable code more easily. And they
  182. have the advantage that the compiler will optimise their program on a new machine 
  183. as well as on the old one.
  184.  
  185.  
  186. Bye...    
  187.     Stephan
  188.  
  189. |> > Stephan
  190. |> 
  191. [sig deleted]
  192.  
  193. -- 
  194. ===============================================
  195. =             Stephan Thesing                 =
  196. =        AG Praktische Informatik             =
  197. =          Technische Fakult"at               =
  198. =         Universit"at Bielefeld              =
  199. ===============================================
  200.